import { ref } from 'vue';
import { getStore } from '@/utils/utils';
const accessRight = ref<string[]>([]);
export default () => {
    const getAccessRight = () => {
        // 移除缓存检查，确保每次都重新加载最新数据
        accessRight.value = getStore('esign-admin-permissionCodes');
    };

    const hasAccess = (accessCode: string) => {
        getAccessRight();
        let index = accessRight.value.findIndex(i => i === accessCode);
        return index !== -1;
    };

    // 支持同时传 code 和 accessType，两个条件都满足才返回 true
    const hasPermission = (code: string, accessType?: number): boolean => {
        const meInfoStr = getStore('sign_admin_userInfo') || '{}';
        let accessRightCodes: { code: string; accessType: number }[] = [];
        try {
            const meInfo = meInfoStr;
            accessRightCodes = meInfo.accessRightCodes || [];
        } catch (e) {
            accessRightCodes = [];
        }
        return accessRightCodes.some(item => {
            const codeMatch = item.code === code;
            const accessTypeMatch = accessType === undefined ? true : item.accessType === accessType;
            return codeMatch && accessTypeMatch;
        });
    };

    // 判断 userRoleType 是否等于传入值
    const isUserRoleType = (roleType: number): boolean => {
        const meInfoStr = getStore('sign_admin_userInfo') || '{}';
        let userRoleType: number | undefined;
        try {
            const meInfo = meInfoStr;
            userRoleType = meInfo.userRoleType;
        } catch (e) {
            userRoleType = undefined;
        }
        return userRoleType === roleType;
    };

    /**
     * 判断用户是否有权限或角色类型  上面hasPermission isUserRoleType两个函数的加强版
     * @param code 权限码（可选）
     * @param accessType 权限类型（可选） 1 有当前传递的参数权限   2 没有当前传递的参数权限
     * @param roleType 用户角色类型（可选），只要匹配就直接返回true  传递的值是1那么就是超级管理员   传递2就是其他就是普通用户
     * @returns boolean
     */

    const hasPermissionOrRole = (code?: string, accessType?: number, roleType?: number): boolean => {
        // console.log(code,accessType,roleType);

        // 只要roleType匹配，直接返回true
        if (roleType !== undefined) {
            const meInfoStr = getStore('sign_admin_userInfo') || '{}';

            let userRoleType: number | undefined;
            try {
                const meInfo = meInfoStr;
                userRoleType = meInfo.userRoleType;
            } catch (e) {
                userRoleType = undefined;
            }
            if (userRoleType === roleType) return true;
        }

        //code和accessType都传时，必须同时满足
        if (code) {
            const meInfoStr = getStore('sign_admin_userInfo') || '{}';
            let accessRightCodes: { code: string; accessType: number }[] = [];
            try {
                const meInfo = meInfoStr;
                accessRightCodes = meInfo.accessRightCodes || [];
            } catch (e) {
                accessRightCodes = [];
            }
            return accessRightCodes.some(item => {
                const codeMatch = item.code === code;
                const accessTypeMatch = accessType === undefined ? true : item.accessType === accessType;
                return codeMatch && accessTypeMatch;
            });
        }
        // 如果都没传，默认false
        return false;
    };

    return {
        getAccessRight,
        accessRight,
        hasPermissionOrRole,
        hasAccess,
        hasPermission,
        isUserRoleType,
    };
};
